--package.path = package.path..";?.lua"
package.path = "./plugin/ZXDU68B301/utils.lua"..";..\\?.lua"
package.path = "./plugin/ZXDU68B301/json.lua"..";..\\?.lua"
package.path = "./plugin/ZXDU68B301/ydt1363.lua"..";..\\?.lua"

require ("utils")
require ("json")
require ("ydt1363")

-- rxBuf = {0x7E,0x32,0x30,0x30,0x31,0x36,0x30,0x30,0x30,0x44,
--          0x30,0x31,0x32,0x31,0x31,0x30,0x33,0x45,0x38,0x30,
--          0x34,0x42,0x30,0x35,0x43,0x33,0x30,0x30,0x32,0x35,
--          0x38,0x46,0x39,0x44,0x45,0x0D}
-- rxBuf = {0x7E,0x31,0x30,0x30,0x31,0x34,0x30,0x30,0x30,0x32,
--          0x30,0x30,0x45,0x30,0x31,0x30,0x31,0x30,0x31,0x20,
--          0x20,0x30,0x31,0x30,0x30,0x30,0x30,0x46,0x42,0x31,
--          0x46,0x0D}
rxBuf = {}
regAddr = 0x0000

function NewVariables()
    -- body
    local VariableMap = {
        Variable = {
        },
    }

    return VariableMap
end

function GenerateGetRealVariables(sAddr,step)

    -- body
    local requestADU = {}
    local requestADULen = 0
    local requestPDU = {}
    local VariableMap = {
        Status = "0",
        Variable = {},
    }

    if(step == 0)
    then
        ----------获取交流开关量输入状态----------------
        requestPDU[1] = 0x10
        requestPDU[2] = sAddr
        requestPDU[3] = 0x40
        requestPDU[4] = 0x43
        local length = ydt1363.GetLENGTHHex(2)
        requestPDU[5] = length[1]
        requestPDU[6] = length[2]
        requestPDU[7] = 0x01

        --获取校验和码
        local requestPDULen = 7
        local chksum = ydt1363.GetCHKSUM(requestPDU,requestPDULen)

        --往缓存中加入SOI
        table.insert(requestADU,0x7E)
        requestADULen = requestADULen + 1

        --往缓存中加入VER~INFO数据
        local requestPDUStr = {}
        requestPDUStr = utils.Hex2Str(requestPDU,requestPDULen)
        for index=1,2*requestPDULen,1 do
            table.insert(requestADU,requestPDUStr[index])
        end
        requestADULen = requestADULen + 2*requestPDULen

        --往缓存中加入CHKSUM
        for index=1,4,1 do
            table.insert(requestADU,chksum[index])
        end
        requestADULen = requestADULen + 4

        --往缓存中加入EOI
        table.insert(requestADU,0x0D)
        requestADULen = requestADULen + 1

        regAddr = 0x4043
        VariableMap = {
            Status = "1",
            Variable = requestADU
        }
    elseif(step == 1)
    then
        ----------获取整理开关量输入状态-----------------
        requestPDU[1] = 0x10
        requestPDU[2] = sAddr
        requestPDU[3] = 0x41
        requestPDU[4] = 0x43
        requestPDU[5] = 0x00
        requestPDU[6] = 0x00

        --获取校验和码
        local requestPDULen = 6
        local chksum = ydt1363.GetCHKSUM(requestPDU,requestPDULen)

        --往缓存中加入SOI
        table.insert(requestADU,0x7E)
        requestADULen = requestADULen + 1

        --往缓存中加入VER~INFO数据
        local requestPDUStr = {}
        requestPDUStr = utils.Hex2Str(requestPDU,requestPDULen)
        for index=1,2*requestPDULen,1 do
            table.insert(requestADU,requestPDUStr[index])
        end
        requestADULen = requestADULen + 2*requestPDULen

        --往缓存中加入CHKSUM
        for index=1,4,1 do
            table.insert(requestADU,chksum[index])
        end
        requestADULen = requestADULen + 4

        --往缓存中加入EOI
        table.insert(requestADU,0x0D)
        requestADULen = requestADULen + 1

        regAddr = 0x4143
        VariableMap = {
            Status = "1",
            Variable = requestADU
        }   
    elseif(step == 2)
    then
        ----------获取整流告警量-----------------
        requestPDU[1] = 0x10
        requestPDU[2] = sAddr
        requestPDU[3] = 0x41
        requestPDU[4] = 0x44
        requestPDU[5] = 0x00
        requestPDU[6] = 0x00

        --获取校验和码
        local requestPDULen = 6
        local chksum = ydt1363.GetCHKSUM(requestPDU,requestPDULen)

        --往缓存中加入SOI
        table.insert(requestADU,0x7E)
        requestADULen = requestADULen + 1

        --往缓存中加入VER~INFO数据
        local requestPDUStr = {}
        requestPDUStr = utils.Hex2Str(requestPDU,requestPDULen)
        for index=1,2*requestPDULen,1 do
            table.insert(requestADU,requestPDUStr[index])
        end
        requestADULen = requestADULen + 2*requestPDULen

        --往缓存中加入CHKSUM
        for index=1,4,1 do
            table.insert(requestADU,chksum[index])
        end
        requestADULen = requestADULen + 4

        --往缓存中加入EOI
        table.insert(requestADU,0x0D)
        requestADULen = requestADULen + 1

        regAddr = 0x4144
        VariableMap = {
            Status = "0",
            Variable = requestADU
        }         
    end

    local tmp = ""
    for i=1,requestADULen,1 do
        tmp = tmp .. " " .. string.format("%02X",requestADU[i])
    end
    print(tmp)
    return VariableMap
end

function DeviceCustomCmd(sAddr,cmdName,cmdParam,step)

    -- body
    local requestADU = {}
    local requestADULen = 0
    local requestPDU = {}
    local VariableMap = {
        Status = "0",
        Variable = {},
    }

    if(cmdName == "GetRealVariables")
    then
        if(step == 0)
        then
            ----------获取交流开关量输入状态----------------
            requestPDU[1] = 0x10
            requestPDU[2] = sAddr
            requestPDU[3] = 0x40
            requestPDU[4] = 0x43
            local length = ydt1363.GetLENGTHHex(2)
            requestPDU[5] = length[1]
            requestPDU[6] = length[2]
            requestPDU[7] = 0xFF

            --获取校验和码
            local requestPDULen = 7
            local chksum = ydt1363.GetCHKSUM(requestPDU,requestPDULen)

            --往缓存中加入SOI
            table.insert(requestADU,0x7E)
            requestADULen = requestADULen + 1

            --往缓存中加入VER~INFO数据
            local requestPDUStr = {}
            requestPDUStr = utils.Hex2Str(requestPDU,requestPDULen)
            for index=1,2*requestPDULen,1 do
                table.insert(requestADU,requestPDUStr[index])
            end
            requestADULen = requestADULen + 2*requestPDULen

            --往缓存中加入CHKSUM
            for index=1,4,1 do
                table.insert(requestADU,chksum[index])
            end
            requestADULen = requestADULen + 4

            --往缓存中加入EOI
            table.insert(requestADU,0x0D)
            requestADULen = requestADULen + 1

            regAddr = 0x4043
            VariableMap = {
                Status = "1",
                Variable = requestADU
            }
        elseif(step == 1)
        then
            ----------获取整理开关量输入状态-----------------
            requestPDU[1] = 0x10
            requestPDU[2] = sAddr
            requestPDU[3] = 0x41
            requestPDU[4] = 0x43
            local length = ydt1363.GetLENGTHHex(2)
            requestPDU[5] = length[1]
            requestPDU[6] = length[2]
            requestPDU[7] = 0xFF

            --获取校验和码
            local requestPDULen = 7
            local chksum = ydt1363.GetCHKSUM(requestPDU,requestPDULen)

            --往缓存中加入SOI
            table.insert(requestADU,0x7E)
            requestADULen = requestADULen + 1

            --往缓存中加入VER~INFO数据
            local requestPDUStr = {}
            requestPDUStr = utils.Hex2Str(requestPDU,requestPDULen)
            for index=1,2*requestPDULen,1 do
                table.insert(requestADU,requestPDUStr[index])
            end
            requestADULen = requestADULen + 2*requestPDULen

            --往缓存中加入CHKSUM
            for index=1,4,1 do
                table.insert(requestADU,chksum[index])
            end
            requestADULen = requestADULen + 4

            --往缓存中加入EOI
            table.insert(requestADU,0x0D)
            requestADULen = requestADULen + 1

            regAddr = 0x4143
            VariableMap = {
                Status = "1",
                Variable = requestADU
            }   
        elseif(step == 2)
        then
            ----------获取整流告警量-----------------
            requestPDU[1] = 0x10
            requestPDU[2] = sAddr
            requestPDU[3] = 0x41
            requestPDU[4] = 0x44
            requestPDU[5] = 0x00
            requestPDU[6] = 0x00

            --获取校验和码
            local requestPDULen = 6
            local chksum = ydt1363.GetCHKSUM(requestPDU,requestPDULen)

            --往缓存中加入SOI
            table.insert(requestADU,0x7E)
            requestADULen = requestADULen + 1

            --往缓存中加入VER~INFO数据
            local requestPDUStr = {}
            requestPDUStr = utils.Hex2Str(requestPDU,requestPDULen)
            for index=1,2*requestPDULen,1 do
                table.insert(requestADU,requestPDUStr[index])
            end
            requestADULen = requestADULen + 2*requestPDULen

            --往缓存中加入CHKSUM
            for index=1,4,1 do
                table.insert(requestADU,chksum[index])
            end
            requestADULen = requestADULen + 4

            --往缓存中加入EOI
            table.insert(requestADU,0x0D)
            requestADULen = requestADULen + 1

            regAddr = 0x4144
            VariableMap = {
                Status = "0",
                Variable = requestADU
            }         
        end                 
    end

    return VariableMap
end

function AnalysisRx(sAddr,rxBufCnt)

    local SOI,VER,ADR,CID1,CID2,LENGTH,LENID,INFO,DATA,CHKSUM,EOI

    -- body
    local VariableMap = {
        Status = "1",
        Variable = {},
    }

    --SOI[1]+VER[2]+ADR[2]+CID1[2]+CID2[2]+LENGTH[4]
    if(rxBufCnt < 11)
    then
        rxBuf = {}
        return VariableMap
    end

    for index=1,rxBufCnt,1 do
        if(index <= rxBufCnt)
        then
            SOI  = rxBuf[index]
            if(SOI==0x7E and rxBufCnt>=13) then
                VER  = utils.Str2Hex(utils.Copy(rxBuf,index+1,2),2)
                ADR  = utils.Str2Hex(utils.Copy(rxBuf,index+3,2),2)
                CID1 = utils.Str2Hex(utils.Copy(rxBuf,index+5,2),2)
                CID2 = utils.Str2Hex(utils.Copy(rxBuf,index+7,2),2)
                LENGTH = utils.Str2Hex(utils.Copy(rxBuf,index+9,4),4)
                LENID = ydt1363.CheckLCHKSUM(LENGTH)
                if(LENID[1]==1 or rxBufCnt<(13+LENID[2])) then
                    rxBuf = {}
                    VariableMap = {
                        Status = "1",
                        Variable = {}
                    }
                    return VariableMap
                end

                --CHKSUM校验
                if(ydt1363.CheckCHKSUM(rxBuf,index+LENID[2]+12) == 1) then
                    rxBuf = {}
                    VariableMap = {
                        Status = "1",
                        Variable = {}
                    }
                    return VariableMap
                end

                print("VER "..string.format("%02X",VER[1]))
                print("ADR "..string.format("%02X",ADR[1]))
                print("CID1 "..string.format("%02X",CID1[1]))
                print("CID2 "..string.format("%02X",CID2[1]))
                print("LENGTH "..string.format("%02X%02X",LENGTH[1],LENGTH[2]))
                print("LCHKSUM "..string.format("%01X",LENID[1]))
                print("LENID "..string.format("%d",LENID[2]))

                INFO = utils.Str2Hex(utils.Copy(rxBuf,index+13,LENID[1]*256+LENID[2]),LENID[1]*256+LENID[2])
                local tmp = ""
                for i=1,LENID[2]/2,1 do
                    tmp = tmp .. " " .. string.format("%02X",INFO[i])
                end
                print(tmp)

                --INFO字段偏移量
                local INFOIndex = 1+12
                --判断设备标识码和控制标识码
                if(CID1[1]==0x40 and CID2[1]==0x00) then
                    if(regAddr == 0x4043) then --获取交流开关量输入状态
                        local Variable = {}
                        local VariableIndex = 1
                        local DATAIndex = 1
                        ------------------------DATAFlag-------------------------------------------------------
                        local DATAFLAG = INFO[DATAIndex]
                        DATAIndex = DATAIndex + 1
                        ------------------------DATANUM-------------------------------------------------------
                        local PackCnt = INFO[DATAIndex]
                        DATAIndex = DATAIndex + 1
                        ------------------------交流输出空开状态------------------------------------------------
                        DATAIndex = DATAIndex + 1
                        ------------------------自定义开关数量-------------------------------------------------------
                        local PackCnt = INFO[DATAIndex]
                        DATAIndex = DATAIndex + 1
                        ------------------------供电状态-------------------------------------------------
                        local PowerState = INFO[DATAIndex]
                        DATAIndex = DATAIndex + 1
                        if(PowerState == 0x00) then
                            Variable[VariableIndex] = utils.AppendVariable(VariableIndex,"PowerState",PowerState,"市电1")
                        elseif(PowerState == 0x01) then
                            Variable[VariableIndex] = utils.AppendVariable(VariableIndex,"PowerState",PowerState,"市电2")
                        elseif(PowerState == 0x02) then
                            Variable[VariableIndex] = utils.AppendVariable(VariableIndex,"PowerState",PowerState,"电池")
                        else
                            Variable[VariableIndex] = utils.AppendVariable(VariableIndex,"PowerState",PowerState,"未知状态")
                        end
                        VariableIndex = VariableIndex + 1
                        ------------------------自定义开关数量-------------------------------------------------------
                        local PackCnt = INFO[DATAIndex]
                        DATAIndex = DATAIndex + 1

                        VariableMap = {
                            Status = "0",
                            Variable = Variable
                        }
                        rxBuf = {}
                        return VariableMap
                    end
                elseif(CID1[1]==0x41 and CID2[1]==0x00) then
                    if(regAddr == 0x4143) then --获取整流开关量输入状态
                        local Variable = {}
                        local VariableIndex = 1
                        local DATAIndex = 1
                        ------------------------DATAFLAG-------------------------------------------------------
                        local DATAFLAG = INFO[DATAIndex]
                        DATAIndex = DATAIndex + 1
                        ------------------------DATANUM-------------------------------------------------------
                        local DATANUM = INFO[DATAIndex]
                        DATAIndex = DATAIndex + 1
                        ------------------------整流器1关机状态------------------------------------------------
                        local RectifierCloseState1 = INFO[DATAIndex]
                        DATAIndex = DATAIndex + 1
                        if(RectifierCloseState1 == 0) then
                            Variable[VariableIndex]  = utils.AppendVariable(VariableIndex,"RectifierCloseState1",RectifierCloseState1,"开机")
                        elseif(RectifierCloseState1 == 1) then
                            Variable[VariableIndex]  = utils.AppendVariable(VariableIndex,"RectifierCloseState1",RectifierCloseState1,"关机")
                        else
                            Variable[VariableIndex]  = utils.AppendVariable(VariableIndex,"RectifierCloseState1",RectifierCloseState1,"未知状态")
                        end
                        VariableIndex = VariableIndex + 1
                        ------------------------整流器1限流状态------------------------------------------------
                        local RectifierLimiteState1 = INFO[DATAIndex]
                        DATAIndex = DATAIndex + 1
                        if(RectifierLimiteState1 == 0) then
                            Variable[VariableIndex]  = utils.AppendVariable(VariableIndex,"RectifierLimiteState1",RectifierLimiteState1,"限流")
                        elseif(RectifierLimiteState1 == 1) then
                            Variable[VariableIndex]  = utils.AppendVariable(VariableIndex,"RectifierLimiteState1",RectifierLimiteState1,"不限流")
                        else
                            Variable[VariableIndex]  = utils.AppendVariable(VariableIndex,"RectifierLimiteState1",RectifierLimiteState1,"未知状态")
                        end
                        VariableIndex = VariableIndex + 1
                        ------------------------整流器1充电方式------------------------------------------------
                        local RectifierChargeType1 = INFO[DATAIndex]
                        DATAIndex = DATAIndex + 1
                        if(RectifierChargeType1 == 0) then
                            Variable[VariableIndex]  = utils.AppendVariable(VariableIndex,"RectifierChargeType1",RectifierChargeType1,"浮充")
                        elseif(RectifierChargeType1 == 1) then
                            Variable[VariableIndex]  = utils.AppendVariable(VariableIndex,"RectifierChargeType1",RectifierChargeType1,"均充")
                        elseif(RectifierChargeType1 == 2) then
                            Variable[VariableIndex]  = utils.AppendVariable(VariableIndex,"RectifierChargeType1",RectifierChargeType1,"测试")
                        else
                            Variable[VariableIndex]  = utils.AppendVariable(VariableIndex,"RectifierChargeType1",RectifierChargeType1,"未知状态")
                        end
                        VariableIndex = VariableIndex + 1
                        ------------------------DATANUM-------------------------------------------------------
                        DATAIndex = DATAIndex + 1
                        ------------------------DATANUM-------------------------------------------------------
                        DATAIndex = DATAIndex + 1
                        ------------------------整流器2关机状态------------------------------------------------
                        local RectifierCloseState2 = INFO[DATAIndex]
                        DATAIndex = DATAIndex + 1
                        if(RectifierCloseState2 == 0) then
                            Variable[VariableIndex]  = utils.AppendVariable(VariableIndex,"RectifierCloseState2",RectifierCloseState2,"开机")
                        elseif(RectifierCloseState2 == 1) then
                            Variable[VariableIndex]  = utils.AppendVariable(VariableIndex,"RectifierCloseState2",RectifierCloseState2,"关机")
                        else
                            Variable[VariableIndex]  = utils.AppendVariable(VariableIndex,"RectifierCloseState2",RectifierCloseState2,"未知状态")
                        end
                        VariableIndex = VariableIndex + 1
                        ------------------------整流器2限流状态------------------------------------------------
                        local RectifierLimiteState2 = INFO[DATAIndex]
                        DATAIndex = DATAIndex + 1
                        if(RectifierLimiteState2 == 0) then
                            Variable[VariableIndex]  = utils.AppendVariable(VariableIndex,"RectifierLimiteState2",RectifierLimiteState2,"限流")
                        elseif(RectifierLimiteState2 == 1) then
                            Variable[VariableIndex]  = utils.AppendVariable(VariableIndex,"RectifierLimiteState2",RectifierLimiteState2,"不限流")
                        else
                            Variable[VariableIndex]  = utils.AppendVariable(VariableIndex,"RectifierLimiteState2",RectifierLimiteState2,"未知状态")
                        end
                        VariableIndex = VariableIndex + 1
                        ------------------------整流器2充电方式------------------------------------------------
                        local RectifierChargeType2 = INFO[DATAIndex]
                        DATAIndex = DATAIndex + 1
                        if(RectifierChargeType2 == 0) then
                            Variable[VariableIndex]  = utils.AppendVariable(VariableIndex,"RectifierChargeType2",RectifierChargeType2,"浮充")
                        elseif(RectifierChargeType2 == 1) then
                            Variable[VariableIndex]  = utils.AppendVariable(VariableIndex,"RectifierChargeType2",RectifierChargeType2,"均充")
                        elseif(RectifierChargeType2 == 2) then
                            Variable[VariableIndex]  = utils.AppendVariable(VariableIndex,"RectifierChargeType2",RectifierChargeType2,"测试")
                        else
                            Variable[VariableIndex]  = utils.AppendVariable(VariableIndex,"RectifierChargeType2",RectifierChargeType2,"未知状态")
                        end
                        VariableIndex = VariableIndex + 1
                        ------------------------DATANUM-------------------------------------------------------
                        DATAIndex = DATAIndex + 1
                        ------------------------DATANUM-------------------------------------------------------
                        DATAIndex = DATAIndex + 1
                        ------------------------整流器3关机状态------------------------------------------------
                        local RectifierCloseState3 = INFO[DATAIndex]
                        DATAIndex = DATAIndex + 1
                        if(RectifierCloseState3 == 0) then
                            Variable[VariableIndex]  = utils.AppendVariable(VariableIndex,"RectifierCloseState3",RectifierCloseState3,"开机")
                        elseif(RectifierCloseState3 == 1) then
                            Variable[VariableIndex]  = utils.AppendVariable(VariableIndex,"RectifierCloseState3",RectifierCloseState3,"关机")
                        else
                            Variable[VariableIndex]  = utils.AppendVariable(VariableIndex,"RectifierCloseState3",RectifierCloseState3,"未知状态")
                        end
                        VariableIndex = VariableIndex + 1
                        ------------------------整流器3限流状态------------------------------------------------
                        local RectifierLimiteState3 = INFO[DATAIndex]
                        DATAIndex = DATAIndex + 1
                        if(RectifierLimiteState3 == 0) then
                            Variable[VariableIndex]  = utils.AppendVariable(VariableIndex,"RectifierLimiteState3",RectifierLimiteState3,"限流")
                        elseif(RectifierLimiteState3 == 1) then
                            Variable[VariableIndex]  = utils.AppendVariable(VariableIndex,"RectifierLimiteState3",RectifierLimiteState3,"不限流")
                        else
                            Variable[VariableIndex]  = utils.AppendVariable(VariableIndex,"RectifierLimiteState3",RectifierLimiteState3,"未知状态")
                        end
                        VariableIndex = VariableIndex + 1
                        ------------------------整流器3充电方式------------------------------------------------
                        local RectifierChargeType3 = INFO[DATAIndex]
                        DATAIndex = DATAIndex + 1
                        if(RectifierChargeType3 == 0) then
                            Variable[VariableIndex]  = utils.AppendVariable(VariableIndex,"RectifierChargeType3",RectifierChargeType3,"浮充")
                        elseif(RectifierChargeType3 == 1) then
                            Variable[VariableIndex]  = utils.AppendVariable(VariableIndex,"RectifierChargeType3",RectifierChargeType3,"均充")
                        elseif(RectifierChargeType3 == 2) then
                            Variable[VariableIndex]  = utils.AppendVariable(VariableIndex,"RectifierChargeType3",RectifierChargeType3,"测试")
                        else
                            Variable[VariableIndex]  = utils.AppendVariable(VariableIndex,"RectifierChargeType3",RectifierChargeType3,"未知状态")
                        end
                        VariableIndex = VariableIndex + 1
                        ------------------------DATANUM-------------------------------------------------------
                        DATAIndex = DATAIndex + 1
                        ------------------------DATANUM-------------------------------------------------------
                        DATAIndex = DATAIndex + 1
                        ------------------------整流器4关机状态------------------------------------------------
                        local RectifierCloseState4 = INFO[DATAIndex]
                        DATAIndex = DATAIndex + 1
                        if(RectifierCloseState4 == 0) then
                            Variable[VariableIndex]  = utils.AppendVariable(VariableIndex,"RectifierCloseState4",RectifierCloseState4,"开机")
                        elseif(RectifierCloseState4 == 1) then
                            Variable[VariableIndex]  = utils.AppendVariable(VariableIndex,"RectifierCloseState4",RectifierCloseState4,"关机")
                        else
                            Variable[VariableIndex]  = utils.AppendVariable(VariableIndex,"RectifierCloseState4",RectifierCloseState4,"未知状态")
                        end
                        VariableIndex = VariableIndex + 1
                        ------------------------整流器4限流状态------------------------------------------------
                        local RectifierLimiteState4 = INFO[DATAIndex]
                        DATAIndex = DATAIndex + 1
                        if(RectifierLimiteState4 == 0) then
                            Variable[VariableIndex]  = utils.AppendVariable(VariableIndex,"RectifierLimiteState4",RectifierLimiteState4,"限流")
                        elseif(RectifierLimiteState4 == 1) then
                            Variable[VariableIndex]  = utils.AppendVariable(VariableIndex,"RectifierLimiteState4",RectifierLimiteState4,"不限流")
                        else
                            Variable[VariableIndex]  = utils.AppendVariable(VariableIndex,"RectifierLimiteState4",RectifierLimiteState4,"未知状态")
                        end
                        VariableIndex = VariableIndex + 1
                        ------------------------整流器4充电方式------------------------------------------------
                        local RectifierChargeType4 = INFO[DATAIndex]
                        DATAIndex = DATAIndex + 1
                        if(RectifierChargeType4 == 0) then
                            Variable[VariableIndex]  = utils.AppendVariable(VariableIndex,"RectifierChargeType4",RectifierChargeType4,"浮充")
                        elseif(RectifierChargeType4 == 1) then
                            Variable[VariableIndex]  = utils.AppendVariable(VariableIndex,"RectifierChargeType4",RectifierChargeType4,"均充")
                        elseif(RectifierChargeType4 == 2) then
                            Variable[VariableIndex]  = utils.AppendVariable(VariableIndex,"RectifierChargeType4",RectifierChargeType4,"测试")
                        else
                            Variable[VariableIndex]  = utils.AppendVariable(VariableIndex,"RectifierChargeType4",RectifierChargeType4,"未知状态")
                        end
                        VariableIndex = VariableIndex + 1
                        ------------------------DATANUM-------------------------------------------------------
                        DATAIndex = DATAIndex + 1
                        ------------------------DATANUM-------------------------------------------------------
                        DATAIndex = DATAIndex + 1
                        ------------------------整流器5关机状态------------------------------------------------
                        local RectifierCloseState5 = INFO[DATAIndex]
                        DATAIndex = DATAIndex + 1
                        if(RectifierCloseState5 == 0) then
                            Variable[VariableIndex]  = utils.AppendVariable(VariableIndex,"RectifierCloseState5",RectifierCloseState5,"开机")
                        elseif(RectifierCloseState5 == 1) then
                            Variable[VariableIndex]  = utils.AppendVariable(VariableIndex,"RectifierCloseState5",RectifierCloseState5,"关机")
                        else
                            Variable[VariableIndex]  = utils.AppendVariable(VariableIndex,"RectifierCloseState5",RectifierCloseState5,"未知状态")
                        end
                        VariableIndex = VariableIndex + 1
                        ------------------------整流器5限流状态------------------------------------------------
                        local RectifierLimiteState5 = INFO[DATAIndex]
                        DATAIndex = DATAIndex + 1
                        if(RectifierLimiteState5 == 0) then
                            Variable[VariableIndex]  = utils.AppendVariable(VariableIndex,"RectifierLimiteState5",RectifierLimiteState5,"限流")
                        elseif(RectifierLimiteState5 == 1) then
                            Variable[VariableIndex]  = utils.AppendVariable(VariableIndex,"RectifierLimiteState5",RectifierLimiteState5,"不限流")
                        else
                            Variable[VariableIndex]  = utils.AppendVariable(VariableIndex,"RectifierLimiteState5",RectifierLimiteState5,"未知状态")
                        end
                        VariableIndex = VariableIndex + 1
                        ------------------------整流器5充电方式------------------------------------------------
                        local RectifierChargeType5 = INFO[DATAIndex]
                        DATAIndex = DATAIndex + 1
                        if(RectifierChargeType5 == 0) then
                            Variable[VariableIndex]  = utils.AppendVariable(VariableIndex,"RectifierChargeType5",RectifierChargeType5,"浮充")
                        elseif(RectifierChargeType5 == 1) then
                            Variable[VariableIndex]  = utils.AppendVariable(VariableIndex,"RectifierChargeType5",RectifierChargeType5,"均充")
                        elseif(RectifierChargeType5 == 2) then
                            Variable[VariableIndex]  = utils.AppendVariable(VariableIndex,"RectifierChargeType5",RectifierChargeType5,"测试")
                        else
                            Variable[VariableIndex]  = utils.AppendVariable(VariableIndex,"RectifierChargeType5",RectifierChargeType5,"未知状态")
                        end
                        VariableIndex = VariableIndex + 1
                        ------------------------DATANUM-------------------------------------------------------
                        DATAIndex = DATAIndex + 1
                        ------------------------DATANUM-------------------------------------------------------
                        DATAIndex = DATAIndex + 1
                        ------------------------整流器6关机状态------------------------------------------------
                        local RectifierCloseState6 = INFO[DATAIndex]
                        DATAIndex = DATAIndex + 1
                        if(RectifierCloseState6 == 0) then
                            Variable[VariableIndex]  = utils.AppendVariable(VariableIndex,"RectifierCloseState6",RectifierCloseState6,"开机")
                        elseif(RectifierCloseState6 == 1) then
                            Variable[VariableIndex]  = utils.AppendVariable(VariableIndex,"RectifierCloseState6",RectifierCloseState6,"关机")
                        else
                            Variable[VariableIndex]  = utils.AppendVariable(VariableIndex,"RectifierCloseState6",RectifierCloseState6,"未知状态")
                        end
                        VariableIndex = VariableIndex + 1
                        ------------------------整流器6限流状态------------------------------------------------
                        local RectifierLimiteState6 = INFO[DATAIndex]
                        DATAIndex = DATAIndex + 1
                        if(RectifierLimiteState6 == 0) then
                            Variable[VariableIndex]  = utils.AppendVariable(VariableIndex,"RectifierLimiteState6",RectifierLimiteState6,"限流")
                        elseif(RectifierLimiteState6 == 1) then
                            Variable[VariableIndex]  = utils.AppendVariable(VariableIndex,"RectifierLimiteState6",RectifierLimiteState6,"不限流")
                        else
                            Variable[VariableIndex]  = utils.AppendVariable(VariableIndex,"RectifierLimiteState6",RectifierLimiteState6,"未知状态")
                        end
                        VariableIndex = VariableIndex + 1
                        ------------------------整流器6充电方式------------------------------------------------
                        local RectifierChargeType6 = INFO[DATAIndex]
                        DATAIndex = DATAIndex + 1
                        if(RectifierChargeType6 == 0) then
                            Variable[VariableIndex]  = utils.AppendVariable(VariableIndex,"RectifierChargeType6",RectifierChargeType6,"浮充")
                        elseif(RectifierChargeType6 == 1) then
                            Variable[VariableIndex]  = utils.AppendVariable(VariableIndex,"RectifierChargeType6",RectifierChargeType6,"均充")
                        elseif(RectifierChargeType6 == 2) then
                            Variable[VariableIndex]  = utils.AppendVariable(VariableIndex,"RectifierChargeType6",RectifierChargeType6,"测试")
                        else
                            Variable[VariableIndex]  = utils.AppendVariable(VariableIndex,"RectifierChargeType6",RectifierChargeType6,"未知状态")
                        end
                        VariableIndex = VariableIndex + 1
                        ------------------------DATANUM-------------------------------------------------------
                        DATAIndex = DATAIndex + 1
                        ------------------------DATANUM-------------------------------------------------------
                        DATAIndex = DATAIndex + 1

                        VariableMap = {
                            Status = "0",
                            Variable = Variable
                        }
                        rxBuf = {}
                        return VariableMap
                    elseif(regAddr == 0x4144) then --获取整流告警信息
                        local Variable = {}
                        local VariableIndex = 1
                        local DATAIndex = 1
                        ------------------------DATAFLAG-------------------------------------------------------
                        local DATAFLAG = INFO[DATAIndex]
                        DATAIndex = DATAIndex + 1
                        ------------------------DATANUM-------------------------------------------------------
                        local DATANUM = INFO[DATAIndex]
                        DATAIndex = DATAIndex + 1
                        ------------------------整流模块1故障------------------------------------------------
                        local ModuleAlarm1 = INFO[DATAIndex]
                        DATAIndex = DATAIndex + 1
                        if(ModuleAlarm1 == 0) then
                            Variable[VariableIndex]  = utils.AppendVariable(VariableIndex,"ModuleAlarm1",ModuleAlarm1,"正常")
                        elseif(ModuleAlarm1 == 1) then
                            Variable[VariableIndex]  = utils.AppendVariable(VariableIndex,"ModuleAlarm1",ModuleAlarm1,"故障")
                        else
                            Variable[VariableIndex]  = utils.AppendVariable(VariableIndex,"ModuleAlarm1",ModuleAlarm1,"未知状态")
                        end 
                        VariableIndex = VariableIndex + 1
                        ------------------------DATANUM-------------------------------------------------------
                        local DATANUM = INFO[DATAIndex]
                        DATAIndex = DATAIndex + 1
                        ------------------------整流模块1通讯故障------------------------------------------------
                        local CommunicationAlarm1 = INFO[DATAIndex]
                        DATAIndex = DATAIndex + 1
                        if(CommunicationAlarm1 == 0) then
                            Variable[VariableIndex]  = utils.AppendVariable(VariableIndex,"CommunicationAlarm1",CommunicationAlarm1,"正常")
                        elseif(CommunicationAlarm1 == 1) then
                            Variable[VariableIndex]  = utils.AppendVariable(VariableIndex,"CommunicationAlarm1",CommunicationAlarm1,"故障")
                        else
                            Variable[VariableIndex]  = utils.AppendVariable(VariableIndex,"CommunicationAlarm1",CommunicationAlarm1,"未知状态")
                        end 
                        VariableIndex = VariableIndex + 1
                        ------------------------DATANUM-------------------------------------------------------
                        local DATANUM = INFO[DATAIndex]
                        DATAIndex = DATAIndex + 1
                        ------------------------整流模块2故障------------------------------------------------
                        local ModuleAlarm2 = INFO[DATAIndex]
                        DATAIndex = DATAIndex + 1
                        if(ModuleAlarm2 == 0) then
                            Variable[VariableIndex]  = utils.AppendVariable(VariableIndex,"ModuleAlarm2",ModuleAlarm2,"正常")
                        elseif(ModuleAlarm2 == 1) then
                            Variable[VariableIndex]  = utils.AppendVariable(VariableIndex,"ModuleAlarm2",ModuleAlarm2,"故障")
                        else
                            Variable[VariableIndex]  = utils.AppendVariable(VariableIndex,"ModuleAlarm2",ModuleAlarm2,"未知状态")
                        end 
                        VariableIndex = VariableIndex + 1
                        ------------------------DATANUM-------------------------------------------------------
                        local DATANUM = INFO[DATAIndex]
                        DATAIndex = DATAIndex + 1
                        ------------------------整流模块2通讯故障------------------------------------------------
                        local CommunicationAlarm2 = INFO[DATAIndex]
                        DATAIndex = DATAIndex + 1
                        if(CommunicationAlarm2 == 0) then
                            Variable[VariableIndex]  = utils.AppendVariable(VariableIndex,"CommunicationAlarm2",CommunicationAlarm2,"正常")
                        elseif(CommunicationAlarm2 == 1) then
                            Variable[VariableIndex]  = utils.AppendVariable(VariableIndex,"CommunicationAlarm2",CommunicationAlarm2,"故障")
                        else
                            Variable[VariableIndex]  = utils.AppendVariable(VariableIndex,"CommunicationAlarm2",CommunicationAlarm2,"未知状态")
                        end 
                        VariableIndex = VariableIndex + 1
                        ------------------------DATANUM-------------------------------------------------------
                        local DATANUM = INFO[DATAIndex]
                        DATAIndex = DATAIndex + 1
                        ------------------------整流模块3故障------------------------------------------------
                        local ModuleAlarm3 = INFO[DATAIndex]
                        DATAIndex = DATAIndex + 1
                        if(ModuleAlarm3 == 0) then
                            Variable[VariableIndex]  = utils.AppendVariable(VariableIndex,"ModuleAlarm3",ModuleAlarm3,"正常")
                        elseif(ModuleAlarm3 == 1) then
                            Variable[VariableIndex]  = utils.AppendVariable(VariableIndex,"ModuleAlarm3",ModuleAlarm3,"故障")
                        else
                            Variable[VariableIndex]  = utils.AppendVariable(VariableIndex,"ModuleAlarm3",ModuleAlarm3,"未知状态")
                        end 
                        VariableIndex = VariableIndex + 1
                        ------------------------DATANUM-------------------------------------------------------
                        local DATANUM = INFO[DATAIndex]
                        DATAIndex = DATAIndex + 1
                        ------------------------整流模块3通讯故障------------------------------------------------
                        local CommunicationAlarm3 = INFO[DATAIndex]
                        DATAIndex = DATAIndex + 1
                        if(CommunicationAlarm3 == 0) then
                            Variable[VariableIndex]  = utils.AppendVariable(VariableIndex,"CommunicationAlarm3",CommunicationAlarm3,"正常")
                        elseif(CommunicationAlarm3 == 1) then
                            Variable[VariableIndex]  = utils.AppendVariable(VariableIndex,"CommunicationAlarm3",CommunicationAlarm3,"故障")
                        else
                            Variable[VariableIndex]  = utils.AppendVariable(VariableIndex,"CommunicationAlarm3",CommunicationAlarm3,"未知状态")
                        end 
                        VariableIndex = VariableIndex + 1
                        ------------------------DATANUM-------------------------------------------------------
                        local DATANUM = INFO[DATAIndex]
                        DATAIndex = DATAIndex + 1
                        ------------------------整流模块4故障------------------------------------------------
                        local ModuleAlarm4 = INFO[DATAIndex]
                        DATAIndex = DATAIndex + 1
                        if(ModuleAlarm4 == 0) then
                            Variable[VariableIndex]  = utils.AppendVariable(VariableIndex,"ModuleAlarm4",ModuleAlarm4,"正常")
                        elseif(ModuleAlarm4 == 1) then
                            Variable[VariableIndex]  = utils.AppendVariable(VariableIndex,"ModuleAlarm4",ModuleAlarm4,"故障")
                        else
                            Variable[VariableIndex]  = utils.AppendVariable(VariableIndex,"ModuleAlarm4",ModuleAlarm4,"未知状态")
                        end 
                        VariableIndex = VariableIndex + 1
                        ------------------------DATANUM-------------------------------------------------------
                        local DATANUM = INFO[DATAIndex]
                        DATAIndex = DATAIndex + 1
                        ------------------------整流模块4通讯故障------------------------------------------------
                        local CommunicationAlarm4 = INFO[DATAIndex]
                        DATAIndex = DATAIndex + 1
                        if(CommunicationAlarm4 == 0) then
                            Variable[VariableIndex]  = utils.AppendVariable(VariableIndex,"CommunicationAlarm4",CommunicationAlarm4,"正常")
                        elseif(CommunicationAlarm4 == 1) then
                            Variable[VariableIndex]  = utils.AppendVariable(VariableIndex,"CommunicationAlarm4",CommunicationAlarm4,"故障")
                        else
                            Variable[VariableIndex]  = utils.AppendVariable(VariableIndex,"CommunicationAlarm4",CommunicationAlarm4,"未知状态")
                        end 
                        VariableIndex = VariableIndex + 1
                        ------------------------DATANUM-------------------------------------------------------
                        local DATANUM = INFO[DATAIndex]
                        DATAIndex = DATAIndex + 1
                        ------------------------整流模块5故障------------------------------------------------
                        local ModuleAlarm5 = INFO[DATAIndex]
                        DATAIndex = DATAIndex + 1
                        if(ModuleAlarm5 == 0) then
                            Variable[VariableIndex]  = utils.AppendVariable(VariableIndex,"ModuleAlarm5",ModuleAlarm5,"正常")
                        elseif(ModuleAlarm5 == 1) then
                            Variable[VariableIndex]  = utils.AppendVariable(VariableIndex,"ModuleAlarm5",ModuleAlarm5,"故障")
                        else
                            Variable[VariableIndex]  = utils.AppendVariable(VariableIndex,"ModuleAlarm5",ModuleAlarm5,"未知状态")
                        end 
                        VariableIndex = VariableIndex + 1
                        ------------------------DATANUM-------------------------------------------------------
                        local DATANUM = INFO[DATAIndex]
                        DATAIndex = DATAIndex + 1
                        ------------------------整流模块5通讯故障------------------------------------------------
                        local CommunicationAlarm5 = INFO[DATAIndex]
                        DATAIndex = DATAIndex + 1
                        if(CommunicationAlarm5 == 0) then
                            Variable[VariableIndex]  = utils.AppendVariable(VariableIndex,"CommunicationAlarm5",CommunicationAlarm5,"正常")
                        elseif(CommunicationAlarm5 == 1) then
                            Variable[VariableIndex]  = utils.AppendVariable(VariableIndex,"CommunicationAlarm5",CommunicationAlarm5,"故障")
                        else
                            Variable[VariableIndex]  = utils.AppendVariable(VariableIndex,"CommunicationAlarm5",CommunicationAlarm5,"未知状态")
                        end 
                        VariableIndex = VariableIndex + 1
                        ------------------------DATANUM-------------------------------------------------------
                        local DATANUM = INFO[DATAIndex]
                        DATAIndex = DATAIndex + 1
                        ------------------------整流模块6故障------------------------------------------------
                        local ModuleAlarm6 = INFO[DATAIndex]
                        DATAIndex = DATAIndex + 1
                        if(ModuleAlarm6 == 0) then
                            Variable[VariableIndex]  = utils.AppendVariable(VariableIndex,"ModuleAlarm6",ModuleAlarm6,"正常")
                        elseif(ModuleAlarm6 == 1) then
                            Variable[VariableIndex]  = utils.AppendVariable(VariableIndex,"ModuleAlarm6",ModuleAlarm6,"故障")
                        else
                            Variable[VariableIndex]  = utils.AppendVariable(VariableIndex,"ModuleAlarm6",ModuleAlarm6,"未知状态")
                        end 
                        VariableIndex = VariableIndex + 1
                        ------------------------DATANUM-------------------------------------------------------
                        local DATANUM = INFO[DATAIndex]
                        DATAIndex = DATAIndex + 1
                        ------------------------整流模块6通讯故障------------------------------------------------
                        local CommunicationAlarm6 = INFO[DATAIndex]
                        DATAIndex = DATAIndex + 1
                        if(CommunicationAlarm6 == 0) then
                            Variable[VariableIndex]  = utils.AppendVariable(VariableIndex,"CommunicationAlarm6",CommunicationAlarm6,"正常")
                        elseif(CommunicationAlarm6 == 1) then
                            Variable[VariableIndex]  = utils.AppendVariable(VariableIndex,"CommunicationAlarm6",CommunicationAlarm6,"故障")
                        else
                            Variable[VariableIndex]  = utils.AppendVariable(VariableIndex,"CommunicationAlarm6",CommunicationAlarm6,"未知状态")
                        end 
                        VariableIndex = VariableIndex + 1
                        ------------------------DATANUM-------------------------------------------------------
                        local DATANUM = INFO[DATAIndex]
                        DATAIndex = DATAIndex + 1
                        VariableMap = {
                            Status = "0",
                            Variable = Variable
                        }
                        rxBuf = {}
                        return VariableMap           
                    end 
                end
            else
                index = index + 1
            end
        else
            rxBuf = {}
            return VariableMap
        end
    end

    rxBuf = {}
    return VariableMap
end

--GenerateGetRealVariables(1,1)
--AnalysisRx(1,32)